package com.weiaibenpao.demo.chislim.hurui.adapte;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.adater.RecyclerUserPicAdapter;
import com.weiaibenpao.demo.chislim.bean.Li_UserInfoResult;
import com.weiaibenpao.demo.chislim.bean.UserBean;
import com.weiaibenpao.demo.chislim.hurui.bean.FunsBean;
import com.weiaibenpao.demo.chislim.hurui.httpClient.ApiClient;
import com.weiaibenpao.demo.chislim.hurui.httpClient.HHttpInterface;
import com.weiaibenpao.demo.chislim.map.customview.CircleImageView;
import com.weiaibenpao.demo.chislim.model.MyModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class UserDeailActivity extends Activity {

    Intent intent;
    int userId = 0;   //要查用户的id，不是自己的
    @BindView(R.id.topBg)
    ImageView topBg;
    @BindView(R.id.icon_iv)
    CircleImageView iconIv;
    @BindView(R.id.icon_sex_tv)
    ImageView iconSexTv;
    @BindView(R.id.icon_user_name_tv)
    TextView iconUserNameTv;
    @BindView(R.id.guanzhuTitle)
    TextView guanzhuTitle;
    @BindView(R.id.guanzhuNum)
    TextView guanzhuNum;
    @BindView(R.id.funsTitle)
    TextView funsTitle;
    @BindView(R.id.funsNum)
    TextView funsNum;
    @BindView(R.id.dontaiTitle)
    TextView dontaiTitle;
    @BindView(R.id.dontaiNum)
    TextView dontaiNum;
    @BindView(R.id.userInfo)
    LinearLayout userInfo;
    @BindView(R.id.photoTitle)
    TextView photoTitle;
    @BindView(R.id.photoLineLayout)
    View photoLineLayout;
    @BindView(R.id.photoRecyclerView)
    RecyclerView photoRecyclerView;
    @BindView(R.id.photoLayout)
    RelativeLayout photoLayout;
    @BindView(R.id.activity_user_deail)
    RelativeLayout activityUserDeail;

    Context context;
    Li_UserInfoResult.DataBean dataBean;
    RecyclerUserPicAdapter adapter;
    @BindView(R.id.isFuns)
    Button isFuns;
    @BindView(R.id.isMessage)
    Button isMessage;
    public HHttpInterface apiStores = ApiClient.getModel().getService();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_deail);
        context = getApplicationContext();
        ButterKnife.bind(this);
        intent = getIntent();
        userId = intent.getIntExtra("userId", 0);
        initData(userId);
    }

    public void initData(int userId) {
        Call<Li_UserInfoResult> call = MyModel.getModel().getService().getUserInfo(userId, UserBean.getUserBean().userId);
        call.enqueue(new Callback<Li_UserInfoResult>() {
            @Override
            public void onResponse(Call<Li_UserInfoResult> call, Response<Li_UserInfoResult> response) {
                if (response.isSuccessful()) {
                    dataBean = response.body().getData();
                    initView();
                }
            }

            @Override
            public void onFailure(Call<Li_UserInfoResult> call, Throwable t) {
                Log.i("屮艸芔茻",t.getMessage());
            }
        });
    }

    public void initView() {
        //图片
        Glide.with(context).load(dataBean.getUser().getUserImage())
                .into(iconIv);

        //判断性别
        if ("男".equals(dataBean.getUser().getUserSex())) {
            iconSexTv.setImageResource(R.mipmap.g_ic_boy);
        } else {
            iconSexTv.setImageResource(R.mipmap.g_girl);
        }

        //用户名称
        iconUserNameTv.setText(dataBean.getUser().getUserSex());

        //关注及粉丝，动态
        guanzhuNum.setText(dataBean.getCares() + "");
        funsNum.setText(dataBean.getFanscount() + "");
        dontaiNum.setText(dataBean.getExtra().getTotalSize() + "");

        //相册
        photoRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL));
        //获取图片
        String[] img = null;
        int n = 0;
        if(dataBean.getHumorlist().size() > 0){
            for (int i = 0; i < dataBean.getHumorlist().size(); i++) {
                if (dataBean.getHumorlist().get(i).getHumorImgUrl().split(",") != null) {
                    img = dataBean.getHumorlist().get(i).getHumorImgUrl().split(",");
                    n = i;
                    break;
                }
            }
            for (int i = n + 1; i < dataBean.getHumorlist().size(); i++) {
                img = concat(img, dataBean.getHumorlist().get(i).getHumorImgUrl().split(","));
            }
        }

        adapter = new RecyclerUserPicAdapter(context, img);
        photoRecyclerView.setAdapter(adapter);

        //判断是否已经关注
        if (dataBean.getIsFans() == "请关注") {
            isFuns.setText(dataBean.getIsFans());
        }else{
            isFuns.setText(dataBean.getIsFans());
        }

        isMessage.setText("发消息");
    }

    /**
     * 数组合并
     *
     * @return
     */
    static String[] concat(String[] a, String[] b) {
        String[] c = new String[a.length + b.length];
        return c;
    }

    @OnClick({R.id.isFuns, R.id.isMessage})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.isFuns:
                Call<FunsBean> call = apiStores.getFunsService(dataBean.getUser().getId(),UserBean.getUserBean().userId);
                call.enqueue(new Callback<FunsBean>() {
                    @Override
                    public void onResponse(Call<FunsBean> call, Response<FunsBean> response) {
                        if(response.isSuccessful()){
                            FunsBean bean = response.body() ;
                            if(bean.getCode() == 0){
                                if(dataBean.getIsFans().equals("请关注")){
                                    dataBean.setIsFans("已关注");
                                }else{
                                    dataBean.setIsFans("请关注");
                                }
                                isFuns.setText(dataBean.getIsFans());
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<FunsBean> call, Throwable t) {

                    }
                });
                break;
            case R.id.isMessage:
                break;
        }
    }
}
